Inside Macintosh: Imaging with QuickDraw

Previous | Chapter Top | Chapter Contents | Next

Optimizing Your Images for Different Graphics Devices

The DeviceLoop procedure searches for graphics devices that intersect your window's drawing region, and it informs your application of each different graphics device it finds. The DeviceLoop procedure provides your application with information about the current device's pixel depth and other attributes. Your application can then choose what drawing technique to use for the current device. For example, your application might use inversion to achieve a highlighting effect on a 1-bit graphics device, and, by using the HiliteColor procedure described in the chapter "Color QuickDraw," it might specify a color like magenta as the highlight color on a color graphics device.

For example, you can call DeviceLoop after calling the Event Manager procedure BeginUpdate whenever your application needs to draw into a window, as shown in Listing 1 .

Listing 1 Using the DeviceLoop procedure

PROCEDURE DoUpdate (window: WindowPtr);
VAR
    windowType := Integer;
    myWindow: LongInt;
BEGIN
    windowType := MyGetWindowType(window);
    CASE windowType OF
    kSimpleRectanglesWindow: {simple case: window with 2 color rectangles}
        BEGIN
            BeginUpdate(window);
            myWindow := LongInt(window); {coerce window ptr for MyDrawingProc}
            DeviceLoop(window^.visRgn, @MyTrivialDrawingProc,
                         myWindow, []);
            EndUpdate;
        END;
    {handle other window types--documents, dialog boxes, etc.--here}
END;

When you use the DeviceLoop procedure, you must supply a handle to a drawing region and a pointer to your own application-defined drawing procedure. In Listing 1 , a handle to the window's visible region and a pointer to an application-defined drawing procedure called MyTrivialDrawingProc are passed to DeviceLoop . For each graphics device it finds as the application updates its window, DeviceLoop calls MyTrivialDrawingProc .

Because DeviceLoop provides your drawing procedure with the pixel depth of the current device (along with other attributes passed to your drawing procedure in the deviceFlags parameter), your drawing procedure can optimize its drawing for whatever type of video device is the current device, as illustrated in Listing 2 .

Listing 2 Drawing into different screens

PROCEDURE MyTrivialDrawingProc (depth: Integer;
                                         deviceFlags: Integer;
                                         targetDevice: GDHandle;
                                         userData: LongInt);
VAR
    window: WindowPtr;
BEGIN
    window:= WindowPtr(userData);
    EraseRect(window^.portRect);
    CASE depth OF
    1:                                      {black-and-white screen}
        MyDraw1BitRects(window);            {draw with ltGray, dkGray pats}
    2:
        MyDraw2BitRects(window);         {draw with 2 of 4 available colors}
    {handle other screen depths here}
END;

© 1997 Apple Computer, Inc.

Previous | Chapter Top | Chapter Contents | Next